package edu.cdnu.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.cdnu.common.constant.MessageConstatnt;
import edu.cdnu.common.result.ResultEntity;
import edu.cdnu.pojo.NoticePojo;
import edu.cdnu.pojo.StudentPojo;
import edu.cdnu.service.CourseService;
import edu.cdnu.service.NoticeService;
import edu.cdnu.service.StudentService;

@RestController
@RequestMapping("/notice")
public class NoticeController {
	
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private StudentService studentService;
	
	/**
	 * 通过课程id或者班级id查询课程通知list
	 * @param courseId
	 * @param classId
	 * @return
	 */
	@GetMapping("/noticeList")
	public ResultEntity<List<NoticePojo>> selectNoticeListByCourseIdOrCLassId(Integer courseId,Integer classId){
		List<NoticePojo> list=noticeService.selectNoticeListByCourseIdOrClassId(courseId, classId);
		if(list!=null) {
			return ResultEntity.createSuccessResult(list);
		}
		return ResultEntity.createFailResult(MessageConstatnt.GET_INFO_FAIL);
	}
	
	/**
	 * 新增或修改课程通知
	 * @param noticePojo
	 * @return
	 */
	@PostMapping("/addOrUpdateNotice")
	public ResultEntity<?> addOrUpdateNotice(@RequestBody NoticePojo noticePojo){
		noticeService.addOrUpdateNotice(noticePojo);
		return ResultEntity.createSuccessResult(null);
	}
	
	/**
	 * 删除课程通知
	 * @param noticeId
	 */
	@DeleteMapping("/deleteNotice/{noticeId}")
	public ResultEntity<?> deleteNoticeByNoticeId(@PathVariable String noticeId) {
		noticeService.deleteNoticeByNoticeId(Integer.valueOf(noticeId));
		return ResultEntity.createSuccessResult(null);
	}
	
	/**
	 * 通过 班级id列表 和 天数 查询最近几天对应课程发布的通知
	 * @param idList
	 * @param day
	 * @return
	 */
	@Deprecated
	@RequestMapping("/nearNotice/{day}")
	public ResultEntity<List<NoticePojo>> selectNoticeByNoticeIdListAndDayNumber(int[] idList,@PathVariable Integer day){
		List<Integer> list=new ArrayList<>();
		for(int i:idList) {
			list.add(i);
		}
		return ResultEntity.createSuccessResult(noticeService.selectNoticeByClassIdListAndDayNumber(list, day));
	}
	/**
	 * 通过 班级id列表 和 天数 查询最近几天对应课程发布的通知
	 * 上面那个方法的重载，通过教师id自己查询上的课
	 * @param teacherId
	 * @param day
	 * @return
	 */
	@GetMapping("/nearNotice/{teacherId}/{day}")
	public ResultEntity<List<NoticePojo>> selectNoticeByTeacherIdAndDayNumber(
			@PathVariable("teacherId") Integer teacherId,@PathVariable("day") Integer day){
		// 先查出这个老师上的课程
		List<Integer> courseIdList=courseService.selectCourseIdListByTeacherId(teacherId);
		// 再返回
		List<NoticePojo> list=noticeService.selectNoticeByCourseIdListAndDayNumber(courseIdList, day);
		return ResultEntity.createSuccessResult(list);
	}
	
	/**
	 * 通过学生id查询出最近7天通知
	 * @param studentId
	 * @param day
	 * @return
	 */
	@GetMapping("/studentNearNotice/{studentId}/{day}")
	public ResultEntity<?> selectNoticeByStudentIdAndDayNumber(@PathVariable("studentId") Integer studentId,@PathVariable("day") Integer day){
		StudentPojo studentPojo=studentService.selectStudentById(studentId);
		List<Integer> classIdList=new ArrayList<>();
		classIdList.add(studentPojo.getClassId());
		List<NoticePojo> list=noticeService.selectNoticeByClassIdListAndDayNumber(classIdList, day);
		return ResultEntity.createSuccessResult(list);
	}
}
